package com.example.onlinemusic.test;

import com.example.onlinemusic.common.common;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Set;

public class ListPage extends common {

    public static String url = "http://127.0.0.1:8080/list.html";

    //3自动在输入框帮我输入账号密码
    public ListPage() {
        super(url);
    }

    //检查查询功能
    public void checksearch(){
        driver.findElement(By.xpath("//*[@id=\"exampleInputName2\"]"));
        driver.findElement(By.xpath("//*[@id=\"submit1\"]"));
    }

    //测试查询功能(成功)
    public void searchSuccess(String s){
        driver.findElement(By.xpath("//*[@id=\"exampleInputName2\"]")).sendKeys(s);
        driver.findElement(By.xpath("//*[@id=\"submit1\"]")).click();
    }

    //测试查询功能失败（没有歌）
    public void searchFail(){
        driver.findElement(By.xpath("//*[@id=\"exampleInputName2\"]")).sendKeys("c");
        driver.findElement(By.xpath("//*[@id=\"submit1\"]")).click();
    }



//    测试操作
    public String operation(String s,int i) throws InterruptedException {
        if(s.equals("喜欢")){
//            选取第一个
            driver.findElement(By.xpath("//*[@id=\"info\"]/tr["+i+"]/td[4]/button[2]")).click();
            System.out.println();
            Thread.sleep(500);
            //作用到Alert弹窗上
            Alert alert = driver.switchTo().alert();
            alert.accept();
            //隐性等待1秒
//            driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));

//        alert.dismiss();
            Thread.sleep(500);
            return null;
        }//*[@id="info"]/tr[1]/td[4]/button[1]

        if(s.equals("删除")){
//            选取第一个
            //*[@id="info"]/tr[2]/td[1]
            String ss=driver.findElement(By.xpath("//*[@id=\"info\"]/tr["+i+"]/td[1]")).getText();
            System.out.println(ss);
            driver.findElement(By.xpath("//*[@id=\"info\"]/tr["+i+"]/td[4]/button[1]")).click();
            Thread.sleep(500);
            Alert alert=driver.switchTo().alert();
            alert.accept();
            Thread.sleep(500);

            return ss;
        }//*[@id="info"]/tr[1]/td[4]/button[1]
        return null;
    }

    //上传操作
    public void load(String s) throws InterruptedException {
        driver.findElement(By.xpath("//*[@id=\"body\"]/div[1]/div[2]/a[2]")).click();
        Thread.sleep(500);
        String curWindow=driver.getWindowHandle();
        Set<String> allwindow=driver.getWindowHandles();
        for (String w:allwindow) {
            if(!w.equals(curWindow)){
                driver.switchTo().window(w);
            }
        }
        Thread.sleep(500);
        driver.findElement(By.xpath("/html/body/form/input[1]")).sendKeys("E:\\javaEE\\springBookMaven\\hifini\\"+s+".mp3");
        Thread.sleep(500);
        driver.findElement(By.xpath("/html/body/form/label/input")).sendKeys(s);
        Thread.sleep(500);
        driver.findElement(By.xpath("/html/body/form/input[2]")).click();
        String curWindow1=driver.getWindowHandle();
        Set<String> allwindow1=driver.getWindowHandles();
        for (String w:allwindow1) {
            if(!w.equals(curWindow1)){
                driver.switchTo().window(w);
            }
        }
    }

    public void switchTo1(){
        driver.findElement(By.xpath("//*[@id=\"body\"]/div[1]/div[2]/a[1]")).click();
        String curwindow=driver.getWindowHandle();
        Set<String> allwindow=driver.getWindowHandles();
        for (String s:allwindow) {
            if(!s.equals(curwindow)){
                driver.switchTo().window(s);
            }
        }
    }
}
